package web;

import pojo.SmbmsUser;
import service.SmbmsUserService;
import service.impl.SmbmsUserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet(value = "/PasswordServlet")
public class ChangePasswordServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String oldPas = req.getParameter("oldPassword");
        String newPas = req.getParameter("newPassword");
        String reNewPas = req.getParameter("reNewPassword");
        System.out.println(oldPas);
        System.out.println(newPas);
        System.out.println(reNewPas);
        if (!(oldPas.equals(newPas))&&newPas.equals(reNewPas)){
            SmbmsUser smbmsUser = new SmbmsUser();
            try {
                smbmsUser.setUserPassword(newPas);
                // 调用业务层接口
                SmbmsUserService smbmsUserService = new SmbmsUserServiceImpl();
                int i = smbmsUserService.updatePassword(smbmsUser);
                if (i>0) {
                    // 修改成功
                    resp.sendRedirect("PasSuccess.jsp");
                }else {
                    resp.sendRedirect("PasError.jsp");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }else{
            resp.sendRedirect("PasError.jsp");
        }

    }
}
